package com.cskaoyan.service.impl;

import com.cskaoyan.bean.GenericData;
import com.cskaoyan.bean.param.BaseParam;
import com.cskaoyan.bean.po.MarketLog;
import com.cskaoyan.bean.po.MarketLogExample;
import com.cskaoyan.mapper.MarketLogMapper;
import com.cskaoyan.service.LogService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author LiangRuiJiang
 * @since 2022/06/07  11:57
 */
@Service
public class LogServiceImpl implements LogService {

    @Autowired
    MarketLogMapper marketLogMapper;

    @Override
    public GenericData<MarketLog> queryLog(BaseParam param, String name) {
        PageHelper.startPage(param.getPage(), param.getLimit());

        MarketLogExample example = new MarketLogExample();
        MarketLogExample.Criteria criteria = example.createCriteria();
        String adminName = null;
        if (StringUtils.isEmpty(name)) {
            adminName = "%%";
        } else {
            adminName = "%" + name + "%";
        }
        criteria.andAdminLike(adminName);
        example.setOrderByClause(param.getSort() + " " +param.getOrder());
        List<MarketLog> marketLogs = marketLogMapper.selectByExample(example);

        PageInfo pageInfo = new PageInfo();
        long total = pageInfo.getTotal();
        int pages = pageInfo.getPages();
        Integer page = param.getPage();
        Integer limit = param.getLimit();
        return GenericData.data(total, pages, limit, page, marketLogs);
    }
}
